/**
 * Created by lixiaodi on 2019/03/25.
 */
mini.parse();
var tree = mini.get("tree1");       //得到树
var grid = mini.get("datagrid1");   //得到表
//初始化
$(function(){
	// //显示树
    treeRequest();
    init();
});
//选择节点
// tree.on("nodeselect", function (e) {
//     var nodes = e.node;
//     if (nodes) {
//         var depId = nodes.pk;
//         var types = nodes.id.substring(0,2);
//         $.ajax({
//             url:ajaxUrl + "/personnerlUser/getPersonnerListBypid.do",
//             type:"POST",
//             data : {
//                 id : depId,
//                 types : types
//             },
//             success : function(data){
//                 if(data.data){
//                     grid.setData(data.data);
//                 }else{
//                     showTips(data.msg);
//                     grid.setData(data.data);
//                 }
//             },
//             error:function(data){
//                 mini.alert("失败");
//             }
//         });
//     } else {
//         grid.setData([]);
//         grid.setTotalCount(0);
//     }
// });
//保存
function SaveData(){
	var selectMenus = grid.getSelecteds();
	console.log(selectMenus);
	if(selectMenus){
		//console.log(selectMenus);
		CloseWindow('ok')
	}else{
		mini.alert('请选择人员');
	}
}
function GetData(){
	var selectMenus = grid.getSelected();
	return selectMenus;
}
//关闭子页面
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
    SaveData();
}
function onCancel(e) {
    CloseWindow("cancel");
}
//加载树的方法
function treeRequest(){

	$.ajax({
        url: ajaxUrl + "/formltems/formTree.do?",
		type:"post",
        data:{
            formListId: 29
        },
		success: function (treeData) {
		    console.log(treeData);
			// tree.setData(data);
            $.ajax({
                url: ajaxUrl + "/formList/findByFormListId.do?formListId="+29,
                type: "post",
                dataType: "json",
                success: function(textFieldData){
                    textField = $.parseJSON(textFieldData.data).selecteds[0].cTableField;
//                        condition = $.parseJSON(textFieldData.data).selecteds[0].cTableName;
                    console.log(textField,'树形textField');
                    tree.set({
                        textField: 'name'
                    });
//                        tree.loadList(treeData, "id", "parent_level");
//                        console.log(tree.getData());
                    tree.setData(treeData);
                    var clickNodeFlag = 0;
                    tree.on("nodeselect", function (e) {
                        console.log(e);
                        clickNodeFlag++;
                        treeSelected = e.selected;
                        nodes = tree.getAncestors(treeSelected);
                        nodes.push(treeSelected);
                        $.ajax({
                            url: ajaxUrl + "/formltems/selectArchives.do",
//                                url:"http://192.168.31.65:8080/bpm/formltems/selectArchives.do",
                            type: "post",
                            dataType: "json",
                            data: {
                                formListId: 29,
                                condition: e.node.tabName,
                                conValue: e.node.id
                            },
                            success: function(data){
                                console.log(data);
                                grid_data = data.data;
                                grid.setData(grid_data);
                            },
                            error : function(data){
                            }
                        });
                    })
                },error : function(data){
                }
            });
		},
		error:function(data){
			mini.alert("失败");
		}
	});
}
function init() {
    $.ajax({
        url:ajaxUrl+ "/user/findperson.do",
        type:"post",
        dataType: "json",
        data: {
            formListId: 29,
            // menuId: menuId
        },
        success: function (data) {
            console.log(data);
            grid.setData(data);
        },
        error:function(data){
            mini.alert("失败");
        }

    })
}